package org.gntc.prayer;

import java.sql.Date;
import java.util.Calendar;
import java.util.Locale;

import org.gntc.prayer.database.PrayerDB;
import org.gntc.prayer.database.PrayerDBInfo.PrayerCategoryTable;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.CheckBox;

public class EditPrayerActivity extends Activity
{
	private PrayerDB mPrayerDB;
	private static final int DATE_START_DIALOG_ID = 10;
	private static final int DATE_END_DIALOG_ID = 20;
	private static final int SAVE_CONFIRM_DIALOG_ID = 30;
	private static final int CANCEL_CONFIRM_DIALOG_ID = 40;
	private Button mDateStartBtn = null;
	private Button mDateEndBtn = null;
	private Button mOKBtn = null;
	private Button mCancelBtn = null;
	private CheckBox mCheckBoxPeriod;
	private EditText mTitle = null;
	private EditText mContent = null;
	private Spinner mCategory = null;
	
	// date and time
    private int mYear_start;
    private int mMonth_start;
    private int mDate_start;
    private int mYear_end;
    private int mMonth_end;
    private int mDate_end;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.edit_prayer_layout);
		
		mTitle = (EditText)findViewById(R.id.input_title);
		mContent = (EditText)findViewById(R.id.input_content);
		mCategory = (Spinner)findViewById(R.id.spinner_select_category);
		initDB();
		initCategorySpinner();
		initBtns();
		initPeriod(); // 120602 dgKIM
		
		final Calendar c = Calendar.getInstance();
		mYear_start = c.get(Calendar.YEAR);
		mMonth_start = c.get(Calendar.MONTH);
		mDate_start = c.get(Calendar.DAY_OF_MONTH);
		mYear_end = c.get(Calendar.YEAR);
	    mMonth_end = c.get(Calendar.MONTH);;
	    mDate_end = c.get(Calendar.DAY_OF_MONTH);
	}

	private void initDB() {
		mPrayerDB = new PrayerDB(EditPrayerActivity.this);
		try {
			mPrayerDB.open();
		} catch (SQLiteException e) {
			e.printStackTrace();
		}
	}
	
	private void initCategorySpinner() {
		Cursor c = mPrayerDB.getAllCategoryName();
		String[] from = new String[] {PrayerCategoryTable.NAME};
		int[] to = {android.R.id.text1};
		SimpleCursorAdapter adapter = new SimpleCursorAdapter(EditPrayerActivity.this, android.R.layout.simple_spinner_item, c, from, to);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		mCategory.setAdapter(adapter);
	}
	
	private void initBtns() {
		mDateStartBtn = (Button)findViewById(R.id.btn_day_from);
		mDateEndBtn = (Button)findViewById(R.id.btn_day_to);
		mDateStartBtn.setOnClickListener(mBtnClickListener);
		mDateEndBtn.setOnClickListener(mBtnClickListener);
		mOKBtn = (Button)findViewById(R.id.ok_btn);
		mCancelBtn = (Button)findViewById(R.id.cancel_btn);
		mOKBtn.setOnClickListener(mBtnClickListener);
		mCancelBtn.setOnClickListener(mBtnClickListener);
	}
	
	private View.OnClickListener mBtnClickListener = new View.OnClickListener()
	{
		
		@Override
		public void onClick (View view)
		{
			// TODO Auto-generated method stub
			switch (view.getId())
			{
			case R.id.btn_day_from :
				showDialog(DATE_START_DIALOG_ID);
				break;
				
			case R.id.btn_day_to :
				showDialog(DATE_END_DIALOG_ID);
				break;
				
			case R.id.ok_btn :
				showDialog(SAVE_CONFIRM_DIALOG_ID);
				break;
				
			case R.id.cancel_btn :
				showDialog(CANCEL_CONFIRM_DIALOG_ID);
				break;

			default :
				break;
			}
		}
	};  
	
	@Override
	protected Dialog onCreateDialog (int id)
	{
		Dialog dialog = null;
		// TODO Auto-generated method stub
		switch (id)
		{
		case DATE_START_DIALOG_ID :
			dialog = new DatePickerDialog(EditPrayerActivity.this, mStartDateSetListener, mYear_start, mMonth_start, mDate_start);
			break;
		case DATE_END_DIALOG_ID :
			dialog = new DatePickerDialog(EditPrayerActivity.this, mEndDateSetListener, mYear_end, mMonth_end, mDate_end);
			break;
		case SAVE_CONFIRM_DIALOG_ID :
			ShowAlertDialog();
			break;
		case CANCEL_CONFIRM_DIALOG_ID :
			ShowAlertDialog();
			break;

		default :
			break;
		}
		
		return dialog;
	}
	
	private DatePickerDialog.OnDateSetListener mStartDateSetListener = new DatePickerDialog.OnDateSetListener()
	{
		
		@Override
		public void onDateSet (DatePicker date_picker, int year, int monthOfYear,
				int dayOfMonth)
		{
			mYear_start = year;
			mMonth_start = monthOfYear;
			mDate_start = dayOfMonth;
			updateDate(DATE_START_DIALOG_ID);
		}
	};
	
	private DatePickerDialog.OnDateSetListener mEndDateSetListener = new DatePickerDialog.OnDateSetListener()
	{
		
		@Override
		public void onDateSet (DatePicker date_picker, int year, int monthOfYear,
				int dayOfMonth)
		{
			mYear_end = year;
			mMonth_end = monthOfYear;
			mDate_end = dayOfMonth;
			updateDate(DATE_END_DIALOG_ID);
		}
	};
	
	private void updateDate (int id)
	{
		Calendar calendar = Calendar.getInstance();
		Date sqldate = null;
		switch (id)
		{
		case DATE_START_DIALOG_ID :
			calendar.set(mYear_start, mMonth_start, mDate_start);
			sqldate = new Date(calendar.getTimeInMillis());
			mDateStartBtn.setText(sqldate.toString() + " (" +calendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.getDefault()) +")");
			break;
			
		case DATE_END_DIALOG_ID :
			calendar.set(mYear_end, mMonth_end, mDate_end);
			sqldate = new Date(calendar.getTimeInMillis());
			mDateEndBtn.setText(sqldate.toString() + " (" +calendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.getDefault()) +")");
			break;

		default :
			break;
		}
	}
	
	private void ShowAlertDialog ()
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(EditPrayerActivity.this);
		builder.setTitle(R.string.save_confirm_title)
		.setMessage(R.string.save_confirm_msg)
		.setIcon(android.R.drawable.ic_dialog_alert)
		.setNegativeButton(android.R.string.cancel, mSaveConfirmDialogOnClickListener)
		.setPositiveButton(R.string.save, mSaveConfirmDialogOnClickListener)
		.setNeutralButton(R.string.discard, mSaveConfirmDialogOnClickListener)
		.show();
	}
	
	DialogInterface.OnClickListener mSaveConfirmDialogOnClickListener = new DialogInterface.OnClickListener()
	{
		
		@Override
		public void onClick (DialogInterface dialog, int which)
		{
			switch (which)
			{
			case DialogInterface.BUTTON_POSITIVE :
				savePrayer();
				finish();
				break;

			case DialogInterface.BUTTON_NEGATIVE :
				dialog.cancel();
				break;

			case DialogInterface.BUTTON_NEUTRAL :
				finish();
				break;

			default :
				break;
			}
		}
	};
	
	public void onBackPressed() {
		ShowAlertDialog();
	};
	
	private void initPeriod()
	{
		mCheckBoxPeriod = (CheckBox)findViewById(R.id.checkbox_period);
		mCheckBoxPeriod.setOnClickListener(new View.OnClickListener()
		{            
			public void onClick(View v) 
			{    
				dateBtnEnable(mCheckBoxPeriod.isChecked());
			}        
		});
		
		dateBtnEnable(mCheckBoxPeriod.isChecked());
	}
	
	private void dateBtnEnable(boolean isEnabled) {
		mDateStartBtn.setEnabled(isEnabled);
		mDateEndBtn.setEnabled(isEnabled);
	}
	
	@Override
	protected void onDestroy ()
	{
		// TODO Auto-generated method stub
		mPrayerDB.close();
		super.onDestroy();
	}
	
	private void savePrayer() {
		Calendar calendar = Calendar.getInstance();
		Date sqldate_start = null;
		Date sqldate_end = null;
		if (mCheckBoxPeriod.isChecked()) {
			calendar.set(mYear_start, mMonth_start, mDate_start);
			sqldate_start = new Date(calendar.getTimeInMillis());
			calendar.set(mYear_end, mMonth_end, mDate_end);
			sqldate_end = new Date(calendar.getTimeInMillis());
		}
		
		mPrayerDB.insertPrayer(mTitle.getText().toString(), mContent.getText().toString(), null, sqldate_start, sqldate_end, mCategory.getSelectedItemPosition(), false);
	}
}